<?php

namespace app\admin\command;

use app\common\controller\Backend;
use app\index\model\Apply;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class Pt extends Backend
{

    protected $relationSearch = true;

    /**
     * User模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with('group')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();
            $list = $this->model
                    ->with('group')
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            foreach ($list as $k => $v)
            {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }

    /**
 * 添加
*/
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");

            if ($params)
            {
//                $params['salt'] = Random::alnum();
//                $params['password'] = md5(md5($params['password']) . $params['salt']);
//                $params['avatar'] = '/uploads/20180415/90ab4868b9a1983488e9040713835220.jpg'; //设置新管理员默认头像。
//                $params['pid'] = $this->auth->id;
                $params['status'] = 'normal';
//                $res = $this->model1->where('id',$this->auth->id)->value('path');
//                $params['path'] = $res."-".$this->auth->id;
                $result = $this->model->save($params);
                if ($result === false)
                {
                    $this->error($this->model->getError());
                }
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    public function pt()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v)
            {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    public function details(){
        $ids = input('ids','');

        $user = $this->model->where(['id'=>$ids])->column('username');
        if ($user[0]){
            $map['belong_id'] = $user[0];
        }else{
            return ['gb'=>'','list'=>''];
        }

        $gb = collection( apply::where($map)->field('count(id) total , FROM_UNIXTIME(create_time,\'%Y-%m-%d\') day ')->order('day desc')->group('day')->select())->toArray();
        $list =  apply::order('create_time desc')->where($map)->select();

        return $this->view->fetch('',['gb'=>$gb,'list'=>$list]);
    }

}
